package com.lx.crm.workbench.web.controller;

import com.lx.crm.commons.contants.Contants;
import com.lx.crm.commons.domain.ReturnObject;
import com.lx.crm.commons.utils.DateUtils;
import com.lx.crm.commons.utils.UUIDUtils;
import com.lx.crm.settings.domain.DicValue;
import com.lx.crm.settings.domain.User;
import com.lx.crm.settings.service.DicValueService;
import com.lx.crm.settings.service.UserService;
import com.lx.crm.workbench.domain.Activity;
import com.lx.crm.workbench.domain.Clue;
import com.lx.crm.workbench.domain.ClueRemark;
import com.lx.crm.workbench.service.ActivityService;
import com.lx.crm.workbench.service.ClueRemarkService;
import com.lx.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 雒鑫
 */
@Controller
public class ClueController {
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueRemarkService clueRemarkService;
    @Autowired
    private ActivityService activityService;

    /**
     * 跳转线索主页面 并将数据放到作用域当中进行数据共享
     *
     * @param request
     * @return
     */
    @RequestMapping("workbench/clue/index.do")
    public String index( HttpServletRequest request) {
        //查询所有的用户
        List<User> userList = userService.queryAllUsers();
        //根据称号查询所属值
        List<DicValue> appellationList = dicValueService.queryDicValueByTypeCode("appellation");
        //根据线索状态查询所属值
        List<DicValue> clueStateList = dicValueService.queryDicValueByTypeCode("clueState");
        //查询线索来源
        List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");


        //将数据保存到request作用域当中
        request.setAttribute("userList", userList);
        request.setAttribute("appellationList", appellationList);
        request.setAttribute("clueStateList", clueStateList);
        request.setAttribute("sourceList", sourceList);

        return "workbench/clue/index";
    }
    @RequestMapping("workbench/clue/queryClueByConditionForPage.do")
    public @ResponseBody Object queryClueByConditionForPage(String fullname,String company,String phone,String source,
                                                            String owner,String mphone,String state,
                                                                int pageNo,int pageSize){
        //封装参数
        Map<String,Object> map=new HashMap<>();
        map.put("fullname",fullname);
        map.put("phone",phone);
        map.put("source",source);
        map.put("company",company);
        map.put("owner",owner);
        map.put("mphone",mphone);
        map.put("state",state);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        //调用service层方法，查询数据
        List<Clue> clueList = clueService.queryClueByConditionForPage(map);
       /* int totalRows=activityService.queryCountOfActivityByCondition(map);*/
        int totalRows = clueService.queryClueCount(map);
        //根据查询结果结果，生成响应信息
        Map<String,Object> retMap=new HashMap<>();
        /*retMap.put("activityList",activityList);*/
        retMap.put("clueList",clueList);
        retMap.put("totalRows",totalRows);
        return retMap;
    }

    /**
     * 保存创建的线索
     */
    @RequestMapping("workbench/clue/saveCreateClue.do")
    @ResponseBody
    public Object saveCreateClue(Clue clue, HttpSession session) {
        User user = (User) session.getAttribute(Contants.SESSION_USER);
        //封装参数
        clue.setId(UUIDUtils.getUUID());
        clue.setCreateTime(DateUtils.formateDateTime(new Date()));
        clue.setCreateBy(user.getId());

        ReturnObject returnObject = new ReturnObject();
        //调用service方法 保存创建的线索
        try {
            int ret = clueService.savaCreateClue(clue);
            if (ret > 0) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            } else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙..请稍后重试..");
            }
        } catch (Exception e) {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙..请稍后重试..");
            e.printStackTrace();
        }
        return returnObject;
    }

    /**
     * 根据id查询线索明细 并进行页面跳转
     * @return
     */
    @RequestMapping("workbench/clue/detailClue.do")
    public  String detailClue(String id,HttpServletRequest request){
        //调用service方法 查询数据
        Clue clue = clueService.queryClueForDetaillById(id);
        List<ClueRemark> remarkList = clueRemarkService.queryClueRemarkForDetailByClueId(id);
        List<Activity> activityList = activityService.queryActivityForDetailByClueId(id);
        //把数据保存到作用域当中
        request.setAttribute("clue",clue);
        request.setAttribute("remarkList",remarkList);
        request.setAttribute("activityList",activityList);
        //请求转发
        return "workbench/clue/detail";

    }
}
